'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 英雄星座
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var heroConstellationConf = require('../../../config/heroConstellationConf.js');
var roleService = require('../../../services/roleService.js');
var constantUtil = require('../../../util/constantUtil.js');
var taskService = require('../../../services/taskService.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 觉醒
 */
Handler.prototype.awaken = function (msg, session, next) {
    var role_id = session.uid;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'copy', 'accrued', 'task', 'daily_task', 'achieve', 'sevenDayActivity'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var copy_model = player_manager.copy;

                // 开放限制--英雄等级
                if (role_model.lv < constantUtil.value('hero_constellation_need_lvl')) {
                    return next(null, {
                        code: Code.USER_NOT_HERO_LV
                    });
                }

                // 副本通关限制开放
                var open_copy_id = constantUtil.value('hero_constellation_need_carbon');
                if (!copy_model.getCopy(+open_copy_id)) {
                    return next(null, {
                        code: Code.USER_NOT_COPY
                    });
                }

                // 是否满足升级条件
                var conf = heroConstellationConf.getConf(role_model.constellation_lv + 1);

                if (!conf) {
                    return next(null, {
                        code: Code.USER_MAX_LV
                    });
                }

                // 判断星数
                if (heroConstellationConf.getNeedStar(role_model.constellation_lv + 1) > role_model.total_copy_star_num) {
                    return next(null, {
                        code: Code.USER_NOT_ENOUGH_STAR
                    });
                }

                // 扣资源
                roleService.subGold(player_manager, moduleType.MDL_BUILD, operationType.OPR_BUILD_UPGRADE, conf[0].cost_point);

                // 升级星座
                role_model.setHeroConstellationLv();

                // 刷新战力
                roleService.updateHeroPower(player_manager);

                taskService.addCompleteHeroConstellation(player_manager);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
